package org.jeecg.modules.kuner.userrec.controller.api;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.exception.JeecgBootException;

import org.jeecg.modules.kuner.userrec.entity.UserRecBase;
import org.jeecg.modules.kuner.userrec.service.IUserRecBaseService;
import org.jeecg.modules.kuner.userrec.service.IUserRecShipService;
import org.jeecg.modules.kuner.userrec.vo.BindRecInfoVO;
import org.jeecg.modules.kuner.userrec.vo.RecommendUserVO;
import org.jeecg.modules.system.entity.SysUser;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
 * @Description:
 * @Author: Planifolia.Van
 * @Date: 2024/6/5 上午9:56
 */
@RestController
@Api(tags = "小程序-推荐相关的接口")
@RequestMapping("/api/recommend")
public class ApiRecommend {
    @Resource
    private IUserRecBaseService userRecBaseService;
    @Resource
    private IUserRecShipService userRecShipService;


    /**
     * 获取我的推荐码信息
     *
     * @return 我的推荐码信息
     */
    @ApiOperation(value = "我的推荐页面", notes = "步骤1：该接口进入页面中调用一次，recCount为推荐人数，recInfo为推荐二维码的base64码信息")
    @GetMapping("/myRecommendCode/{memberId}")
    public Result<Map<String, Object>> myRecommendCode(@PathVariable String memberId) {
        UserRecBase userRecBase = userRecBaseService.getUserRecInfo(memberId);
        long recCount = userRecShipService.getMemberRecCount(memberId);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("recCount", recCount);
        resultMap.put("recInfo", userRecBase);
        return Result.OK("获取成功！", resultMap);
    }

    /**
     * 根据邀请码获取用户信息
     *
     * @param recCode 扫码得到的邀请码
     * @return 插叙到的用户信息
     */
    @ApiOperation(value = "根据邀请码获取用户信息", response = SysUser.class, notes = "步骤2：被推荐者扫描二维码的同时，拿到二维码中的recCode后调用该接口，得到推荐人的信息。")
    @GetMapping("/getMemberByRecCode/{recCode}")
    public Result<?> getMemberByRecCode(@PathVariable String recCode) {
        SysUser userBase = Optional.ofNullable(userRecBaseService.getUserByRecCode(recCode)).orElseThrow(() -> new JeecgBootException(String.format("无法根据邀请码：%s，找到对应的会员信息！", recCode)));
        return Result.OK("查询成功！", userBase);
    }

    /**
     * 扫码人绑定推荐信息
     * @return 绑定结果
     */
    @ApiOperation(value = "绑定推荐", notes = "步骤3：从步骤2获取到推荐人的id后，调用该接口，将推荐人的ID传入参数masterId中完成绑定！")
    @PutMapping("/bindRecommendShip")
    public Result<?> bindRecommendShip(@RequestBody BindRecInfoVO bindRecInfoVO) {
        userRecShipService.bindRecommendShip(bindRecInfoVO.getUserId(), bindRecInfoVO.getMasterId());
        return Result.OK("绑定成功！");
    }

    /**
     * 我的直推
     *
     * @return 查询到的我的直推信息
     */
    @ApiOperation(value = "我的推荐", response = RecommendUserVO.class, notes = "该接口用于查看我的推荐")
    @GetMapping("/myRecommend/{memberId}")
    public Result<?> myRecommend(@PathVariable String memberId) {
        RecommendUserVO recommendMemberVO = userRecShipService.myRecommend(memberId);
        return Result.OK("我的推荐信息", recommendMemberVO);
    }








}
